package com.gzdh.tang.modu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzdh.tang.config.ali.AgreementNo;
import com.gzdh.tang.config.ali.AliPayUtil;
import com.gzdh.tang.modu.entity.AliCardAgreement;
import com.gzdh.tang.modu.mapper.AliCardAgreementMapper;
import com.gzdh.tang.modu.service.AliCardAgreementService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
//@Service
@Service("aliCardAgreementServiceImpl")
public class AliCardAgreementServiceImpl extends ServiceImpl<AliCardAgreementMapper, AliCardAgreement> implements AliCardAgreementService {

    @Autowired
    AliCardAgreementMapper aliCardAgreementMapper;

    @Override
    public void saveAgreement(String cardno, String price, int addr, String order) {
        try {
            List<AgreementNo> agreementNos = AliPayUtil.getAgreementNo();
            AgreementNo agreementNo = agreementNos.stream().filter(agreementNo1 -> agreementNo1.getAddress() == addr).findFirst().orElse(null);
            if (agreementNo == null) {
                return;
            }
            Map<String, String> agreementNoMap = aliCardAgreementMapper.getFk(agreementNo.getField(), cardno);
            if (agreementNoMap == null) {
                return;
            }
            AliCardAgreement aliCardAgreement = new AliCardAgreement();
            aliCardAgreement.setAliUid(agreementNoMap.get("AliUserId"));
            aliCardAgreement.setPrice(price);
            aliCardAgreement.setFkPrice(price);
            aliCardAgreement.setAliAgreementNo(agreementNoMap.get("aliAgreementNo"));
            aliCardAgreement.setOrders(order);
            aliCardAgreement.setAddr(addr);
            this.save(aliCardAgreement);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getAgreement(int addr, String cardno) {
        try {
            List<AgreementNo> agreementNos = AliPayUtil.getAgreementNo();
            AgreementNo agreementNo = agreementNos.stream().filter(agreementNo1 -> agreementNo1.getAddress() == addr).findFirst().orElse(null);
            if (agreementNo == null) {
                return null;
            }
            Map<String, String> agreementNoMap = aliCardAgreementMapper.getFk(agreementNo.getField(), cardno);
            if (agreementNoMap == null) {
                return null;
            }
            return agreementNoMap.get("aliAgreementNo");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
